import { ExtractJwt, Strategy } from 'passport-jwt'
import { PassportStrategy } from '@nestjs/passport'
import { Injectable } from '@nestjs/common'
import { ConfigService } from 'src/common/config/config.service'
import { PrismaService } from 'src/common/prisma/prisma.service'
import { User } from '@prisma/client'

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
   constructor(private configService: ConfigService, private readonly prisma: PrismaService) {
      super({
         jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
         ignoreExpiration: false,
         secretOrKey: configService.get('app_secret')
      })
   }

   async validate({ id }) {
      const res = await this.prisma.user.findFirst({
         where: {
            id: +id
         }
      })
      return res as User
   }
}
